package com.music.common;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.music.action.BaseKey;
import com.music.domain.User;

/**
 * 构建参数相关工具类
 * @author LFSenior
 *
 */
public class ParamUtil {
	
	/**
	 * 封装构建工具
	 * @param request
	 * @return
	 */
	public static Map<String,Object> buildRequest(HttpServletRequest request) throws Exception{
			Map<String,Object> reParam=new HashMap<>();
			Enumeration<String> names = request.getParameterNames();
			while(names.hasMoreElements()){
				String name=names.nextElement();
				reParam.put(name, request.getParameter(name));
			}
			return reParam;
	}
	
	/**
	 * 获取用户id
	 * @param request
	 * @return
	 */
	public static String buildUserId(HttpServletRequest request){
		User loginUser=null;
		try {
			 loginUser= (User) request.getSession().getAttribute(BaseKey.LOGIN_USER);
			 if(loginUser!=null){
				 return loginUser.getId();
			 }else{
				 return "1";
			 }
		} catch (Exception e) {
			e.printStackTrace();
			return "1";
		}
	}
	
	/**
	 * 获取用户名
	 * @param request
	 * @return
	 */
	public static String buildUserName(HttpServletRequest request){
		User loginUser=null;
		try {
			 loginUser= (User) request.getSession().getAttribute(BaseKey.LOGIN_USER);
			 if(loginUser!=null){
				 return loginUser.getName();
			 }else{
				 return "LFSenior";
			 }
		} catch (Exception e) {
			e.printStackTrace();
			return "LFSenior";
		}
	}

	
	/**
	 * 获取16位的uuid
	 * @return
	 */
	public static String buildUUID(){
		int machineId = 1;//最大支持1-9个集群机器部署  
        int hashCodeV = UUID.randomUUID().toString().hashCode();  
        if(hashCodeV < 0) {//有可能是负数  
            hashCodeV = - hashCodeV;  
        }  
        return machineId + String.format("%015d", hashCodeV);  
	}
}
